package com.cskaoyan.javase.network._1udp._v2;

import com.cskaoyan.javase.network.util.UDPNetworkUtils;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

/**
 * 发送端
 * @since 10:48
 * @author wuguidong@cskaoyan.onaliyun.com
 */
public class Sender {
    public static void main(String[] args) throws IOException {
        // 1.创建发送端套接字对象,DatagramSocket对象
        DatagramSocket senderSocket = new DatagramSocket();
        // 2.封装数据报包,创建DatagramPacket对象,包含ip地址和端口号,这里的IP地址和端口号必须和接收端的"码头"套接字IP地址,端口号一样
        DatagramPacket sendPacket = UDPNetworkUtils.getSendPacket("今天中午吃什么?", "127.0.0.1", 9527);
        // 3.用DatagramSocket对象的成员方法-send方法,发送数据报包
        senderSocket.send(sendPacket);
        // 4.close关闭资源
        senderSocket.close();
    }
}
